{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# \ud83d\udcdd Exercise M6.04\n",
    "\n",
    "The aim of the exercise is to get familiar with the histogram\n",
    "gradient-boosting in scikit-learn. Besides, we will use this model within a\n",
    "cross-validation framework in order to inspect internal parameters found via\n",
    "grid-search.\n",
    "\n",
    "We will use the California housing dataset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.datasets import fetch_california_housing\n",
    "\n",
    "data, target = fetch_california_housing(return_X_y=True, as_frame=True)\n",
    "target *= 100  # rescale the target in k$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First, create a histogram gradient boosting regressor. You can set the trees\n",
    "number to be large, and configure the model to use early-stopping."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will use a grid-search to find some optimal parameter for this model. In\n",
    "this grid-search, you should search for the following parameters:\n",
    "\n",
    "* `max_depth: [3, 8]`;\n",
    "* `max_leaf_nodes: [15, 31]`;\n",
    "* `learning_rate: [0.1, 1]`.\n",
    "\n",
    "Feel free to explore the space with additional values. Create the grid-search\n",
    "providing the previous gradient boosting instance as the model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, we will run our experiment through cross-validation. In this regard,\n",
    "define a 5-fold cross-validation. Besides, be sure to shuffle the data.\n",
    "Subsequently, use the function `sklearn.model_selection.cross_validate` to run\n",
    "the cross-validation. You should also set `return_estimator=True`, so that we\n",
    "can investigate the inner model trained via cross-validation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now that we got the cross-validation results, print out the mean and standard\n",
    "deviation score."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then inspect the `estimator` entry of the results and check the best\n",
    "parameters values. Besides, check the number of trees used by the model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Inspect the results of the inner CV for each estimator of the outer CV.\n",
    "Aggregate the mean test score for each parameter combination and make a box\n",
    "plot of these scores."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write your code here."
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "main_language": "python"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}